package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.AppUtils;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.BitmapUtils;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.ChoiceFromAlbum;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.io.IOException;

public class HomeActivity extends AppCompatActivity {

    private Uri imageUri;
    private final int ALBUM_CODE=1;
    private final int CAMERA_CODE=2;
    ImageView imageView;
    private Bitmap bitmap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        AppUtils.verifyStoragePermissions(this);
        init();
    }

    private void init() {
        //final Intent intent=new Intent(this,TrailService.class);

       Button button= (Button) findViewById(R.id.trail);
        imageView= (ImageView) findViewById(R.id.testimage);
        button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //startService(intent);
                 GoCameraSeletedImage();

             }
         });
    }

    private void GoCameraSeletedImage() {
        File file = new File(this.getExternalCacheDir(), "avatar_image.jpg");

        try {
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(Build.VERSION.SDK_INT>=24){//判断版本
            imageUri = FileProvider.getUriForFile(this, "com.getFormPhoto.fileprovider", file);
        }else {
            imageUri= Uri.fromFile(file);
        }
        Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(intent,CAMERA_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case CAMERA_CODE:
                if(resultCode==RESULT_OK){
                    bitmap = ChoiceFromAlbum.getBitmapFromCamera(this, imageUri);
                    if(bitmap ==null){
                        Log.i("bitmapString","-----bitmap == null-----");
                    }
                    imageView.setImageBitmap(bitmap);
                    File file = BitmapUtils.savaiamge(bitmap,HomeActivity.this);
                    pushBitmapToService(file);

                }
                break;
        }
    }

    private void pushBitmapToService(File file ){
        Log.i("bitmapString","-----7777777777-----");
        if(file == null){
            Log.i("bitmapString","---file--null-----");
            return;
        }

        HttpParams params = new HttpParams();
        params.put("image",file);
        Log.i("bitmapString","-----888888888-----");
        OkGo.<String>post("http://112.74.173.191/index.php/Home/salesman/photo_upload").params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                Log.i("bitmapString","-----onSuccess-----"+body);
            }

            @Override
            public void onError(Response<String> response) {

                String body = response.body();
                Log.i("bitmapString","-----onError-----"+body);
            }
        });




    }
}
